﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using NPMonitor.Implementation;
using NPMonitor.DataModel;
using NPMonitor.Web.Code;

namespace NPMonitor.Web.UserControls
{
    public partial class WorkflowList : System.Web.UI.UserControl
    {
        #region Properties

        protected int PageSize
        {
            get { return ctPager.PageSize; }
        }

        #endregion

        #region Methods
        public void BindData(int pageIndex)
        {
            int workflowTotalCount = StoreManager.Current.WorkflowStore.GetCountofWorkflow();
            //如果pageInfex等于-1，则是当前页面
            if (pageIndex == -1)
            {
                pageIndex = ctPager.CurrentPageIndex;
            }
            ctPager.CurrentPageIndex = pageIndex;
            ctPager.IntialProperties(workflowTotalCount);
            lblWorkflowCount.InnerText = workflowTotalCount.ToString();
            lblLRTime.InnerText = DateTime.Now.ToFormatString();
            if (pageIndex > ctPager.TotalPage - 1)
            {
                pageIndex = ctPager.TotalPage - 1;
            }
            List<WorkflowData> list = StoreManager.Current.WorkflowStore.GetAllWorkflow(pageIndex, PageSize);
            rptWorkflowList.DataSource = list;
            rptWorkflowList.DataBind();

        }

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            ctPager.PageChange += new PagerControl.PageRefresh(BindData);
            if (!IsPostBack)
            {
                BindData(0);
            }
            phWorkflowInfo.Visible = false;
            phSaveSuccessful.Visible = false;
        }

        protected void imgRefresh_OnClick(object sender, EventArgs e)
        {
            BindData(ctPager.CurrentPageIndex);
        }

        protected void rptWorkflowList_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                if (e.CommandName == "Edit")
                {
                    string id = e.CommandArgument.ToString();
                    ucWorkflowInfo.WorkflowId = int.Parse(id);
                    ucWorkflowInfo.ShowWorkflowInfo(int.Parse(id));
                    phWorkflowInfo.Visible = true;
                }
            }
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                if (e.CommandName == "Delete")
                {
                    string id = e.CommandArgument.ToString();
                    try
                    {
                        StoreManager.Current.WorkflowStore.DeleteWorkflow(int.Parse(id));
                    }
                    catch
                    {
                        JsUtil.Alert(CommonString.SaveFailedTip, this.Page);
                    }
                    BindData(ctPager.CurrentPageIndex);
                    phSaveSuccessful.Visible = true;
                }
            }
        }

        protected void lbtnCreateWorkflow_Click(object sender, EventArgs e)
        {
            phWorkflowInfo.Visible = true;
            ucWorkflowInfo.ClearControlText();
        }

        protected void rptWorkflowList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            WorkflowData data = e.Item.DataItem as WorkflowData;

            if (data != null)
            {
                LinkButton lbtnDeleteWorkflow = e.Item.FindControl("lbtnDeleteWorkflow") as LinkButton;

                lbtnDeleteWorkflow.OnClientClick = string.Format("return WorkflowVerification({0})", data.WorkflowId);
            }
        }

        public void ShowSuccessfulDiv()
        {
            phSaveSuccessful.Visible = true;
        }

    }
}